package metalink;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;
import java.sql.*;

import org.apache.struts2.ServletActionContext;

import vo.*;
import vo.mixed.*;
import model.admin.*;
import myutil.*;
import java.io.*;

public class Inbox extends ActionSupport{
	
ArrayList<Message_Receiver_MixedVo> unread=new ArrayList<Message_Receiver_MixedVo>();
ArrayList<Message_Receiver_MixedVo> read=new ArrayList<Message_Receiver_MixedVo>();
public ArrayList<Message_Receiver_MixedVo> getUnread() {
	return unread;
}
public void setUnread(ArrayList<Message_Receiver_MixedVo> unread) {
	this.unread = unread;
}
public ArrayList<Message_Receiver_MixedVo> getRead() {
	return read;
}
public void setRead(ArrayList<Message_Receiver_MixedVo> read) {
	this.read = read;
}
public String execute(){
	try {
		String username=ServletActionContext.getRequest().getSession().getAttribute("username").toString();
		Connect c=new Connect();
		ResultSet rs=c.getResult("select * From message m, message_receiver mr where mr.message_id=m.message_id and mr.receiver_id='"+username+"' and mr.read_flag=0 and mr.deleted_flag!=1 order by sent_at desc");
		while(rs.next()){
			int attachment_id,attachment_count=0;
			Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
			single_object.setMessage_id(rs.getInt("message_id"));
			single_object.setSubject(rs.getString("subject"));
			single_object.setBody(rs.getString("body"));
			single_object.setSender_id(rs.getString("sender_id"));
			single_object.setSent_at(rs.getTimestamp("sent_at").toString());
			attachment_id=rs.getInt("attachment_id");
			File f=new File("C:/Users/SIVA KRISHNA/workspace/CampusMetalink/WebContent/attachments/"+attachment_id+"/");
			if(attachment_id!=0)
				attachment_count=f.list().length;
			single_object.setAttachment_count(attachment_count);
			unread.add(single_object);
		}
		rs=c.getResult("select * From message m, message_receiver mr where mr.message_id=m.message_id and mr.receiver_id='"+username+"' and mr.read_flag=1 and mr.deleted_flag!=1 order by sent_at desc");
		while(rs.next()){
			int attachment_id,attachment_count=0;
			Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
			single_object.setMessage_id(rs.getInt("message_id"));
			single_object.setSubject(rs.getString("subject"));
			single_object.setBody(rs.getString("body"));
			single_object.setSender_id(rs.getString("sender_id"));
			single_object.setSent_at(rs.getTimestamp("sent_at").toString());
			attachment_id=rs.getInt("attachment_id");
			File f=new File("C:/Users/SIVA KRISHNA/workspace/CampusMetalink/WebContent/attachments/"+attachment_id+"/");
			if(attachment_id!=0)
				attachment_count=f.list().length;
			single_object.setAttachment_count(attachment_count);
			read.add(single_object);
		}
		return SUCCESS;
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return 	ERROR;
	}
}
}
